Flash Article

Bike Trips Sample Application: Using the XMLConnector Component in Flash MX Professional 2004

Leo Schuman, J.D.
Macromedia Regional Master Instructor
Codepattern Software Services

Macromedia Flash has been happily consuming XML packets since Flash 5. However, now you can take XML data and display it in Flash easier than ever using the XMLConnector component and the new dynamic data binding features in Flash MX Professional 2004. In this tutorial, I present one way to quickly and easily create an XML data-driven interface in Flash MX Professional 2004, without using a single line of hand-written ActionScript. Of course, you could also do everything here using hand-written ActionScript, but that approach is not convenient for all Flash developers. Flash MX Professional 2004 now gives you a choice.

This tutorial covers:

  • Configuring the XMLConnector component and its schema
  • Binding complex data to the ComboBox component
  • Dynamically binding text data to TextInput and TextArea components
  • Formatting dynamically bound data
  • Dynamically binding images to the Loader component

This tutorial uses materials from one section of the new, comprehensive Macromedia Authorized Training course:

Macromedia Authorized Training: Flash MX 2004: Application Development

Sign up for this course now ›

Requirements

To complete this tutorial you will need to install the following software and files:

Macromedia Flash MX Professional 2004


Download sample files for this tutorial


Level of Experience Required: Intermediate

To complete this tutorial successfully:

  • You are familiar with Flash.
  • You've seen XML and understand why data-driven web applications are useful.

In the tutorial, you will learn what data binding means in Flash MX 2004, and how dynamic data binding helps components respond to user input.

Getting Started: Examining the Bike Trips Application and Its Data

This application reads bike tour information from an external XML file, and then displays this information for the user. The user may select any tour by name and see the cost, description, and a related graphic. These values update each time the selected trip changes. (The sample is visually simple because the tutorial focuses on component functionality rather than visual design.)

  1. Download XMLConnector_tutorial.zip and unzip it to a local directory.
  2. Notice the files and directory structure:

    • Finished application: solution/XMLConnector _done.fla
    • Unfinished application: XMLConnector _raw.fla
    • Data: data/trips.xml
    • Graphics:

      • images/bigsur.jpg
      • images/grandcanyon.jpg
      • images/greatsmokies.jpg
      • images/natchez.jpg
      • images/vermont.jpg
      • images/napa.jpg
    • Application in staged finishing points for your use and review:

      • solution/XMLConnector_step1_done.fla
      • solution/XMLConnector_step2_done.fla
      • solution/XMLConnector_step3_done.fla
      • solution/XMLConnector_step4_done.fla
      • solution/XMLConnector_step5_done.fla

  3. Open XMLConnector_done.fla in FlashMX Professional 2004.

  4. Select Control > Test Movie to test the application.
  5. As you test, change the Trip name in the ComboBox component to see the graphics and data update.

    Note: In production, your SWF, XML, and JPG files reside in your document root (e.g., /wwwroot) directory. However, you do not need to use a local web server to complete this tutorial.

  6. Open the file data/trips.xml and examine the XML data structure.

    Note: You may need to change the "Files of Type" pop-up menu in the Open dialog box to "All Files (*.*)" to see this file.

  7. Close solution/XMLConnector_done.fla but leave data/trips.xml open to re-examine later.

Configuring the Application to Use the XMLConnector Component

To use the XMLConnector component, you place a component instance in your application, point it at the data source XML file, and add a behavior that triggers the component to load data as the application loads.

  1. Open XMLConnector _raw.fla in Flash MX 2004 and save it as XMLConnector_step1.fla.
  2. Take a look at the layer structure; note that only the background and static text layers have any content.

    Note: Although the document includes a code layer, you will add code as a behavior, so you won't have to hand code. Nonetheless, for good file organization, it's a recommended practice to attach code to a distinct layer, rather than to visual content layers.

  3. Open the Components panel and drag and drop an instance of the XMLConnector component into your application, placing it just offstage to the left of the static TextField containing the words Bike Trip Central.

    Dragging the XMLConnector component into your application.

    Figure 1. Dragging the XMLConnector component into your application.

    Note: The XMLConnector component would be invisible at runtime if placed on the Stage, but it might interfere with other visual development. So, I recommended that you place it offstage.

  4. With the XMLConnector component selected, open the Property inspector, click the Parameters tab, and set the following values:

    • Instance Name: xcTrips
    • URL: data/trips.xml
    • Direction: receive
  5. Leave the remaining properties at their default settings:

    • ignoreWhite: true
    • multipleSimultaneousAllowed: true
    • suppressInvalidCalls: false

    Note: In general, ignore white space when reading XML, as you may not be able to control whether your source XML includes white space for readability (as does trips.xml) or does not. The other two settings are relevant to sending XML data through the component, which you will not do in this tutorial.

  6. Add a Trigger Data Source behavior to the first frame of the code layer, to trigger the XMLConnector component to load its data:

    1. Select the first frame of the code layer.
    2. Open the Behaviors panel.
    3. Press the Plus (+) button to add a new behavior.
    4. Select Data > Trigger Data Source.
    5. Select the xcTrips component from the Select Data Source component dialog box.

    This behavior adds the following line of code to the code layer:

    this.xcTrips.trigger();

    Behaviors are a new Flash MX 2004 feature. They attach prewritten code to your application. This particular behavior causes the XMLConnector component, which you named xcTrips, to execute its trigger() method and load its data from trips.xml when the playhead executes the first frame of this one-frame application. Once you add the behavior, you will see a lowercase "a" appear in the frame, indicating that code is attached to it.

  7. Test your application to verify data is loading:

    1. Select Control > Test Movie to test the application.
    2. Select Debug > List Variables to examine variables available in the application.
    3. Press Control-F and search for the variable called:

      level0.xcTrips.results

    4. Examine the XML data structure in this variable; note that it matches the values in trips.xml.
  8. Import data/trips.xml as the schema for xcTrips:

    1. Select xcTrips.
    2. Open the Component Inspector panel.
    3. Select the Schema tab.
    4. Select the results:XML property.
    5. Press the "Import a schema from a sample XML file" button, in the upper right corner of the Schema tab.
    6. Open the /data directory, select trips.xml, and press Open.

      Importing data/trips.xml as the Schema for xcTrips.

      Figure 2. Importing data/trips.xml as the schema for xcTrips.

  9. Using the Name tabs above the Stage, toggle between XMLConnector_step1.fla and data/trips.xml. Compare the imported data schema in results:XML property of xcTrips to the XML data in data/trips.xml. Notice the following:

    XML file: data/trips.xml Component Inspector panel schema for xcTrips (imported from data/trips.xml)
    The entire XML packet in data/trips.xml is wrapped in a single trips tag. The root of the imported schema is a property called trips of the data type Object (represented as trips:Object).
    The XML packet contains a series of sections—or nodes—each called trip. The trips object contains a property called trip, which is a series—or Array—of an indefinite number of objects.
    Each trip node has an attribute called name plus four nodes nested within it: description, cost, tapes, and image. Each object in trip:Array has five properties: name, description, cost, tapes, and image. Name is an attribute of the object (signified by the @ symbol); three properties have string data, and two have integer (numeric) data.
  10. Save your changes to XMLConnector_step1.fla.

Binding XML Data to a ComboBox Component

  1. Save XMLConnector_step1.fla as XMLConnector_step2.fla.
  2. Open the Components panel, drag an instance of the ComboBox component onto the Stage, and drop it below the word "Trip."
  3. Open the Property inspector for this new ComboBox instance and set the following values:

    • Instance name: cbTrips
    • Width: 120

    Note: While you can set static, default labels and data in the Property inspector, do not do so here as the XMLConnector component will dynamically provide the labels and data for this ComboBox.

  4. Expose the array of trip objects from the xcTrips XMLConnector as an available binding:

    1. Select xcTrips.
    2. Open the Component Inspector panel and select the Bindings tab.
    3. Click the Plus (+) button to add a new data binding, exposing the imported XML data schema, which you previously bound to xcTrips.
    4. Select the array of trip objects (called trip:Array) from the schema and press OK.
    5. You should now see that the results.trips.trip property of xcTrips listed as a binding attached to xcTrips.
  5. Attach the newly exposed binding to the dataProvider property of the cbTrips ComboBox:

    1. Select the results.trips.trip binding from the Bindings tab in the Component Inspector panel for the xcTrips XMLConnector.
    2. In the Bindings property grid, below the binding, set the direction property for this binding to out.
    3. Select the bound to property and press the magnifying glass tool button.
    4. In the Bound To dialogue, set the following:

      • Component path: ComboBox <cbTrips>
      • Schema location: dataProvider : Array

    Data bindings are data connections between components that Flash maintains automatically. A binding may be one-directional or two-directional. In this case, you've created a one-directional binding going "out" from the trip:Array in xcTrips (xcTrips:results.trips.trip) and "in" to the cbTrips ComboBox. Because a ComboBox can only display one String as a label for each row, you must to specify which values from the trip:Array should be the labels for the cbTrips ComboBox.

Creating ComboBox Labels Using XML Data

Format the inbound data from the xcTrips XMLConnector so the cbTrips ComboBox knows to display the name property from each object in the inbound array as the label property for each of its own rows:

  1. Select cbTrips.
  2. Open the Bindings tab of the Component Inspector panel.
  3. Select the dataProvider binding.
  4. Set the binding's direction to in, as it is inbound from xcTrips:results:trips:trip.
  5. Set the formatter property for this binding to Rearrange Fields.
  6. Select the formatter options property and press the magnifying glass button to open the Rearrange Fields dialog box.
  7. Set the Fields definitions to label=name and click OK.

    You've exposed complex data—an array of objects, in which each object has five properties (a data structure you could analogize to a table where each row has five columns)—from the xcTrips XMLConnector component. But, because a ComboBox can only expose one value as a label for each row, you must specify what specific outbound property to use as the value for each label. Although we do not do so here, it's also possible to concatenate multiple values as a single String in the label.

  8. Select Control > Test Movie to test your application. The name value from each Object in the trip:Array outbound from xcTrips should now appear as the label for each row of the cbTrips ComboBox.

    View of ComboBox showing labels from xcTrips

    Figure 3. View of ComboBox showing labels from xcTrips

  9. Save your changes to XMLConnector_step2.fla.

Displaying and Updating a Trip Description in a TextArea Component

  1. Save XMLConnector_step2.fla as XMLConnector_step3.fla.
  2. Open the Components panel, drag an instance of the TextArea component onto the Stage, and drop it below the word "Description."
  3. Open the Property inspector for this new TextArea instance and set the following values:

    • Instance Name: taDescription
    • Width: 275
    • Height: 75
    • Editable: false

    Note: While you can set default text (even HTML-formatted text) for a TextArea component, do not do so here because the xcTrips XMLConnector component will provide these values dynamically, and users will specify them using the cbTrips ComboBox.

  4. Expose the description property from trip:Array in xcTrips as an available binding:

    1. Select xcTrips.
    2. Open the Component Inspector panel and select the Bindings tab.
    3. Press the Plus (+) button to add a new data binding, exposing the imported XML data schema, which you previously bound to xcTrips.
    4. Select the description property in the array of trip objects (called trip:Array) from the schema and click OK.
    5. You should now see the results.trips.trip.[n].description property of xcTrips listed as a binding available from xcTrips.
  5. Attach the newly exposed binding to the text property of the taDescription TextArea component:

    1. Select the xcTrips XMLConnector component. Select the results.trips.trip.[n].description binding from the Bindings tab in the Component Inspector panel.
    2. In the Bindings property grid, below the binding, set the direction property for this binding to "out."
    3. Select the bound to property and click the magnifying glass button.
    4. In the Bound To dialog box, set the following:

      • Component Path: TextArea <taDescription>
      • Schema Location: text : String
    5. Press OK.
  6. Select Control > Test Movie to test your application. Notice that the description for the first trip now appears in the taDescription TextArea component. However, if you select a different value in cbTrips, the description displayed in taDescription does not yet update.

    taDescription TextArea component with napa text.

    Figure 4. taDescription TextArea component with napa text.

  7. To make the currently selected row in cbTrips determine which description displays in taDescription, use the selectedIndex property in cbTrips to pull the appropriate description from the array of trip objects in xcTrips. Because data bindings dynamically update when bound values change, the description will update whenever the trip name changes:

    1. Select the xcTrips XMLConnector component.
    2. Open the Component Inspector panel and select the Bindings tab.
    3. Select the results.trips.trip.[n].description binding.
    4. Select the Index for 'trip' property, and press the magnifying glass button./li>
    5. In the Bound Index dialogue, uncheck "use a constant value" and set the following:

      • Component Path: cbTrips ComboBox
      • Schema Location: selectedIndex : Number

    Bindings for xcTrips.

    Figure 5. Bindings for xcTrips.

  8. Note: If you test the application at this point, the description will be undefined until a user changes cbTrips once, then it will update as expected. You will next set a default selectedIndex value to ensure that data binds to taDescription when the application first loads.

  9. Set the default value of the selectedIndex property of cbTrips to zero, making this value available to drive the data binding for taDescription when the application first loads:

    1. Select the cbTrips ComboBox component.
    2. Open the Components Inspector panel and select the Schema tab.
    3. Select the selectedIndex:Number property.
    4. In the property grid set the default value to 0.
    5. Select Control > Test Movie to test your application. You should see the trip, "Napa," and its description "The incredible variety of Napa …" when the movie loads. As you change the trip name, the description should automatically update.
    6. Save your changes to XMLConnector_step3.fla.

Displaying and Updating a Formatted Cost in a TextInput Component

  1. Save XMLConnector_step3.fla as XMLConnector_step4.fla.
  2. Open the Components panel. Drag an instance of the TextInput component onto the Stage and drop it below the word "Cost."
  3. Open the Property inspector for this new TextArea instance and set the following values:

    • Instance name: tiCost
    • Width: 135
    • Editable: false

    Note: While you can set default text for a TextInput component, do not do so here because the xcTrips XMLConnector component will provide these values dynamically, and users will specify them using the cbTrips ComboBox.

  4. Expose the cost property from trip:Array in xcTrips as an available binding:

    • Select xcTrips.
    • Open the Component Inspector panel and select the Bindings tab.
    • Click the Plus (+) button to add a new data binding, exposing the imported XML data schema, which you previously bound to xcTrips.
    • Select the cost property in the array of trip objects (called trip:Array) from the schema and click OK.
    • You should now see the results.trips.trip.[n].cost property of xcTrips listed as a binding available from xcTrips.
  5. Attach the newly exposed binding to the text property of tiCost TextInput component:

    • Select the results.trips.trip.[n].cost binding from the Bindings tab in the Component Inspector panel for the xcTrips XMLConnector component instance.
    • In the Bindings property grid, below the binding, set the direction property for this binding to "out."
    • Select the bound to property, and click the magnifying glass button.
    • In the Bound To dialogue, set the following:

      • Component Path: TextInput <tiCost>
      • Schema Location: text : String
    • Click OK.
  6. Select Control > Test Movie to test your application. Notice that the cost for the first trip now appears in the tiCost TextInput component. However, if you select a different value in cbTrips, the cost displayed in tiCost does not yet update. Notice also that the cost is unformatted.
  7. Use the selectedIndex property of the cbTrips ComboBox component to make the currently selected row determine the cost displayed in the tiCost TextInput component. Because data bindings dynamically update when bound values change, the cost will update whenever the trip name changes:

    1. Select the xcTrips XMLConnector component.
    2. Open the Component Inspector panel and select the Bindings tab.
    3. Select the results.trips.trip.[n].cost binding.
    4. Select the Index for 'trip' property, and click the magnifying glass button.
    5. In the Bound Index dialogue, uncheck "use a constant value" and set the following:

      • Component Path: cbTrips ComboBox
      • Schema Location: selectedIndex : Number

    Note: At this point in the last series of steps, set a default value of zero for cbTrips' selectedIndex property, to ensure data binding occurs when the application first loads, just as though the user had selected that index of the ComboBox. You do not need to repeat this here, as it's effective for all components bound to the cbTrips ComboBox.

  8. Format the data in the tiCost TextInput component so that it appears as United States currency:

    1. Select the tiCost TextInput component.
    2. Open the Component Inspector and select the Bindings tab.
    3. Set the formatter property to "Compose String."
    4. Select formatter options, and press the magnifying glass button.
    5. In the Compose String dialogue, set the following value: $<.>.00.
    6. Click OK.

    Note: When using formatters, refer to dynamic values using brackets. If a particular field receives complex data, use the property name in brackets. If a particular field receives simple data, such as a String or Number (as you do here), use a single dot (".") in brackets to refer to the actual value received. You then "compose" the rest of the string around those dynamic values however you wish (as you did above).

    Note: Flash MX Professional 2004 has tools and methods to support localized data formatting. However, that discussion is beyond the scope of this tutorial.

  9. Select Control > Test Movie to test your application. You should see the trip "Napa," its description, and its cost, formatted as "$1095.00" as your application loads. As you change the trip name, the cost and description should automatically update.
  10. Save your changes to XMLConnector_step4.fla.

Displaying and Updating a Graphic in a Loader Component

  1. Save XMLConnector_step4.fla as XMLConnector_step5.fla.
  2. Open the Components panel, drag an instance of the Loader component onto the Stage, and drop it below the words "Bike Trip Central," and above the other components.
  3. Open the Property inspector for this new Loader instance, and set the following values:

    • Instance Name: loImage
    • Width: 297
    • Height: 85
    • X: 2

    Note: While you can set default contentPath for a Loader component, do not do so here because the xcTrips XMLConnector will provide this path dynamically, and users will specify changes to the path through their selections in the cbTrips ComboBox. Because you've set a default selectedIndex of 0 for the ComboBox, data will bind as the movie first loads and the image for the first ComboBox item (napa.jpg) will appear.

  4. Expose the image property from trip:Array in xcTrips as an available binding:

    1. Select xcTrips.
    2. Open the Component Inspector panel and select the Bindings tab.
    3. Click the Plus (+) button to add a new data binding, exposing the imported XML data schema, which you previously bound to xcTrips.
    4. Select the image property in the array of trip objects (called trip:Array) from the schema and click OK.
    5. You should now see the results.trips.trip.[n].image property of xcTrips listed as a binding available from xcTrips.
  5. Attach the newly exposed binding to the contentPath property of the loImage Loader component:

    1. Select the results.trips.trip.[n].image binding from the Bindings tab in the Component Inspector panel for the xcTrips XMLConnector.
    2. In the Bindings property grid, below the binding, set the direction property for this binding to "out."
    3. Select the bound to property, and press the magnifying glass button.
    4. In the Bound To dialogue, set the following:

      • Component Path: Loader <loImage>
      • Schema Location: contentPath : String

  6. Select Control > Test Movie to test your application. Notice that the image for the first trip (napa.jpg) now appears in the loImage Loader component. However, if you select a different value in cbTrips, the image does not yet update.
  7. Once again, use the selectedIndex property of the cbTrips ComboBox component to make the currently selected row trigger the correct image to display in the loImage Loader component. Do this by using the selectedIndex property to dynamically determine the correct image property from the array of trip objects in xcTrips to bind to the Loader component. Because data bindings dynamically update when bound values change, the image will update whenever the trip name changes:

    1. Select the xcTrips XMLConnector.
    2. Open the Component Inspector panel and select the Bindings tab.
    3. Select the results.trips.trip.[n].image binding.
    4. Select the Index for 'trip' property, and press the magnifying glass button.
    5. In the Bound Index dialogue, uncheck "use a constant value" and set the following:

      • Component Path: cbTrips ComboBox
      • Schema Location: selectedIndex : Number

    Note: Again, because you previously set a default value of zero for cbTrips' selectedIndex property, data binds when the application first loads, just as though a user had selected that index of the ComboBox. So, all data binding tied to cbTrips will occur on load, including this new binding to the Loader component.

  8. Select Control > Test Movie to test your application. You should see the trip "Napa," its description, its formatted cost, and the graphic napa.jpg as you load. As you change the trip name, the cost, description, and images should automatically update.
  9. Save your changes to XMLConnector_step5.fla.

Summary

In this tutorial, you've looked at a number of new features of Flash MX Professional 2004, most importantly, the XMLConnector component and dynamic data binding. Along the way, you've used the Component Inspector panel, component schemas (including customized schemas imported for the XMLConnector), and have gotten a taste of using some complex data types, such as arrays and objects. And there's a lot more out there.

Flash MX Professional 2004 is well-poised to become the dominant rich web application development platform. This will only happen if its developer community seizes the opportunity. If this tutorial whets your appetite, visit http://www.macromedia.com/support/training/ and check out all the instructor-led learning opportunities available.

Related Articles and Resources

Once again, this article is an excerpt from the following Macromedia Authorized Training course:

Macromedia Authorized Training: Flash MX 2004: Application Development

Sign up for this course now ›

Some related Developer Center of interest to Flash MX Professional 2004 users who are integrating data into applications include:


About the author

Fifteen years doing technical training, writing, and presentation, coupled with geek credentials dating back to the Sinclair ZX81 (and later the Apple II+) have made Leo a leading Macromedia Flash and ColdFusion instructor throughout the United States over the past five years. Leo provides training internally and externally for Macromedia and its training partners, including Ace Communications in Portland, Oregon. He also works regularly with Macromedia products—"practicing what he preaches"—through his steady web application and courseware development practice. You reach Leo through Codepattern Software Services at leo@codepattern.com.